package com.wwwarehouse.taskcenter.fragment.creater_code;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.PreviewPositionCodeAdapter;
import com.wwwarehouse.taskcenter.bean.PreviewWareCodeBean;
import com.wwwarehouse.taskcenter.bean.QueryWareBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/20 10:56
 * @描述 ${TODO}
 */

public class PreviewPositionCodeFragmentChild extends BaseFragment {

    private View mView;
    private StateLayout mStateLayout;
    private GridView mGridView;
    private int                            mPage;
    private Map<String, Object>            mCodeMap;
    private long                         mDataCodeRuleUkid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_preview_warehouse_code_child, container, false);
        initView();
        initData();
        return mView;
    }

    private void initData() {

        if (getArguments() != null) {
            Bundle bundle = getArguments();
            mPage = bundle.getInt("page");
            ArrayList<Parcelable> dataList = bundle.getParcelableArrayList("dataList");
            mDataCodeRuleUkid = bundle.getLong("dataCodeRuleUkid");
            mCodeMap = new HashMap<>();
            getPreviewCode();
        }

    }

    private void getPreviewCode() {
        mCodeMap.put("dataCodeRuleUkid", mDataCodeRuleUkid);
        QueryWareBean queryWareBean = new QueryWareBean();
        queryWareBean.setPage(mPage);
        queryWareBean.setSize(15);
        mCodeMap.put("query", queryWareBean);
        mStateLayout.setAllContentViewMargin(ConvertUtils.dip2px(getActivity(), 11), ConvertUtils.dip2px(getActivity(), 5), ConvertUtils.dip2px(getActivity(), 11), 0);
        mStateLayout.showProgressView(true);
        NoHttpUtils.httpPost(TaskCenterConstant.DATA_CODE, mCodeMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener=new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            try {
                if ("0".equals(commonClass.getCode())) {
                    PreviewWareCodeBean previewWareCodeBean = JSON.parseObject(commonClass.getData().toString(), PreviewWareCodeBean.class);
                    ArrayList<PreviewWareCodeBean.ListBean> list = (ArrayList<PreviewWareCodeBean.ListBean>) previewWareCodeBean.getList();
                    PreviewPositionCodeAdapter previewPositionCodeAdapter = new PreviewPositionCodeAdapter(mActivity, list, mGridView);
                    mGridView.setAdapter(previewPositionCodeAdapter);
                } else {
                    mStateLayout.showSystemView(commonClass.getMsg(), true);
                    mStateLayout.setSystemListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getPreviewCode();
                        }
                    });
                }

            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(true);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getPreviewCode();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    private void initView() {

        mStateLayout = findView(mView, R.id.sl_layout);
        mGridView = findView(mView, R.id.gv);
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}
